//
//  LPSearchViewController.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 17/5/10.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPSearchViewController.h"
#import "GTPageController.h"
#import "LPNewsSearchViewController.h"
#import "LPStudySearchViewController.h"
#import "LPActivitySearchViewController.h"
@interface LPSearchViewController ()<UITextFieldDelegate>
/**  搜索框  */
@property (strong, nonatomic) UITextField  *searchTextField;
/**    */
@property (strong, nonatomic) GTPageController  *gtPageVc;
/**  搜索文字  */
@property (strong, nonatomic) UILabel  *searchText;

@end

@implementation LPSearchViewController

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"searchContent"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigView addSubview:self.searchTextField];
    self.gtPageVc = [self getDefaultController];
    self.gtPageVc.view.frame = CGRectMake(0, 64  , self.view.width, [UIScreen zj_screenHeight] - 64);
    [self.view addSubview:self.gtPageVc.view];
    [self addChildViewController:self.gtPageVc];
    
    [self.navigView addSubview:self.searchText];
}



#pragma mark - 私有方法
- (GTPageController *)getDefaultController{
    
    LPStudySearchViewController *studty = [[LPStudySearchViewController alloc]init];
    
    
     LPActivitySearchViewController *act = [[LPActivitySearchViewController alloc]init];
     LPNewsSearchViewController *news = [[LPNewsSearchViewController alloc]init];
    
    NSArray *titles = @[@"新闻",@"学习",@"活动"];
    NSArray *viewControllers = @[[news class],[studty class],[act class]];
    
    __block NSMutableArray *titlesWidth = [NSMutableArray array];
    [titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
         CGFloat width = [NSString sizeOfString:obj font:16 * [UIScreen zj_textFontScale]  width:self.view.width].width + 30 * [UIScreen zj_screenWScale];
        [titlesWidth addObject:@(width)];
    }];
    
    GTPageController *pageVC = [[GTPageController alloc] initWithViewControllerClasses:viewControllers andTheirTitles:titles];
    pageVC.pageAnimatable = YES;
    pageVC.itemsWidths = titlesWidth;
    pageVC.postNotification = YES;
    pageVC.menuBGColor = [UIColor whiteColor];
    pageVC.menuViewStyle = GTMenuViewStyleLine;
    pageVC.progressColor = [UIColor themeColor];
    pageVC.titleColorSelected = [UIColor themeColor];
    pageVC.titleColorNormal = [UIColor textBlackColor];
    pageVC.titleSizeNormal = 16 * [UIScreen zj_textFontScale];
    pageVC.titleSizeSelected = 17 * [UIScreen zj_textFontScale];
    pageVC.menuHeight = 50 * [UIScreen zj_screenHScale];
    pageVC.selectIndex = self.number;
    
    return pageVC;
}

- (void)action_leftButton:(UIButton *)sender{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"searchContent"];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField{
    /**  发送通知  */
    [[NSNotificationCenter defaultCenter] postNotificationName:@"post" object:self userInfo:@{@"text": textField.text}];
    /**  针对学习和活动切换刷新界面处理  */
    [[NSUserDefaults standardUserDefaults] setObject:textField.text forKey:@"searchContent"];

    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];

    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    if (toBeString.length >= 50 && range.length!= 1 ){
        textField.text = [toBeString substringToIndex:50];
        return NO;
        
    }
    return YES;
}

- (UITextField *)searchTextField{
    if (!_searchTextField) {
        _searchTextField = ({
            UITextField *textField =  [[UITextField alloc] initWithFrame:CGRectMake(55 * [UIScreen zj_screenWScale], 28, [UIScreen zj_screenWith] - 110 * [UIScreen zj_screenWScale], 28)];
            textField.placeholder = @"输入关键字快速查找内容";
            textField.font = [UIFont systemFontOfSize:14 * [UIScreen zj_textFontScale]];
            textField.textColor = [UIColor textBlackColor];
            textField.textAlignment = NSTextAlignmentLeft;
            UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 25, 17)];
            image.image = [UIImage imageNamed:@"home_icon_search"];
            image.contentMode = UIViewContentModeCenter;
            textField.leftView = image;
            textField.leftViewMode = UITextFieldViewModeAlways;
            [textField setTextFieldRightforWidth:10 * [UIScreen zj_screenWScale]];
            textField.backgroundColor = [UIColor whiteColor];
            textField.layer.cornerRadius = 12;
            textField.layer.masksToBounds = YES;
            textField.delegate = self;
            textField.returnKeyType = UIReturnKeySearch;
            [textField becomeFirstResponder];
            textField;
        });
        
    }
    return _searchTextField;
    
}



@end
